package com.invengo.module_robot.data.repsitory.source.impl;

import com.invengo.base.http.RetrofitClient;
import com.invengo.base.http.RobotBaseBean;
import com.invengo.base.http.RobotRxUtils;
import com.invengo.base.utils.RequestBodyUtils;
import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_robot.ActivitBean;
import com.invengo.library_db.entity.module_robot.RobotTokenBean;
import com.invengo.module_robot.data.api.RobotApi;
import com.invengo.module_robot.data.entity.ConfigBean;
import com.invengo.module_robot.data.entity.DouBanBeans;
import com.invengo.module_robot.data.entity.PopularBean;
import com.invengo.module_robot.data.entity.SearchBean;
import com.invengo.module_robot.data.repsitory.source.RobotSource;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.http.BaseBean;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;
import okhttp3.ResponseBody;

public class HttpRobotSourceImpl implements RobotSource {
    RobotApi robotApi;
    public HttpRobotSourceImpl() {
        robotApi = RetrofitClient.getInstance().create(RobotApi.class);
        ConfigBean bean = (ConfigBean) ACache.getInstance().getAsObject(AKey.ROBOT_CONFIG);
        if (bean == null) {
            bean = new ConfigBean(ACache.getInstance().getAsString(AKey.CACHE_HOST), "admin", "123qwe","7795");
        }
        saveConfig(bean);
    }
    @Override
    public Observable<RobotBaseBean<SearchBean>> searchBooks(String keyWord)  {
        return robotApi.searchBook( keyWord);
    }

    @Override
    public Observable<RobotBaseBean<SearchBean>> getBookInfoByIsbn(String isbn) {
        return robotApi.getBookInfoByIsbn(isbn);
    }

    private String userName ="";
    private String password = "";

    @Override
    public Observable<RobotBaseBean<RobotTokenBean>> login(String UserNameOrEmailAddress, String Password) {
        HashMap<String,Object> params =new HashMap<>();
        params.put("userNameOrEmailAddress",userName);
        params.put("password",password);
        return robotApi.login(ACache.getInstance().getAsString(AKey.ROBOT_TENANT_ID), RequestBodyUtils.parse(params));
    }

    @Override
    public Observable<RobotBaseBean<ActivitBean>> getActivities() {
        return robotApi.getActivities()
                .retryWhen(new RobotRxUtils.RetryWithNewToken(login(userName,password),2,1000));
    }

    @Override
    public Observable<ResponseBody> getActivityImage(String attachmentId) {
        return robotApi.getActivityImage(attachmentId);
    }

    @Override
    public Observable<RobotBaseBean<List<PopularBean>>> getPopularBook() {
        return robotApi.getPopularBook()
                .retryWhen(new RobotRxUtils.RetryWithNewToken(login(userName,password),2,1000));
    }

    @Override
    public Observable<RobotBaseBean<List<DouBanBeans>>> getImages(List<String> isbn) {
        return robotApi.getImages(isbn);
    }

    @Override
    public void saveConfig(ConfigBean bean) {
        this.userName= bean.getUserName();
        this.password =bean.getPassWord();
        ACache.getInstance().put(AKey.ROBOT_CONFIG,bean);
        ACache.getInstance().put(AKey.ROBOT_TENANT_ID,bean.getTenantId());
    }
}
